package com.lcu.student.service.impl;

import com.lcu.Result.Result;
import com.lcu.student.entity.PageBean;
import com.lcu.student.entity.StudentGrades;
import com.lcu.student.entity.Subject;
import com.lcu.student.mapper.SubjectMapper;
import com.lcu.student.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 查询所有科目及编号
     * @return
     */
    @Override
    public Result selectSubject() {
        List<StudentGrades> list = subjectMapper.selectSubject();
        return Result.success(list);
    }

    /**
     * 带条件的分页查询
     * @param currentPage
     * @param pageSize
     * @param subject
     * @return
     */
    @Override
    public Result selectByPage(int currentPage, int pageSize, Subject subject) {
        int begin = (currentPage-1)*pageSize;
        int size=pageSize;

        //将数据进行处理，方便进行模糊查询
        String subjectName=subject.getSubjectName();
        if (subjectName != null && subjectName.length()!=0){
            subject.setSubjectName("%"+subjectName+"%");
        }
        String subjectNumber=subject.getSubjectNumber();
        if (subjectNumber != null && subjectNumber.length()!=0){
            subject.setSubjectNumber("%"+subjectNumber+"%");
        }

        //进行查询
        List<Subject> row = subjectMapper.selectByPage(begin,size,subject);

        //查询出总数
        int totalCount = subjectMapper.selectTotalCount(subject);

        //将数据存入PageBean
        PageBean<Subject> pageBean=new PageBean<>();
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(row);

        return Result.success(pageBean);
    }

    /**
     * 添加数据
     * @param subject
     * @return
     */
    @Override
    public Result addSubject(Subject subject) {
        subjectMapper.addSubject(subject);
        return Result.success();
    }

    /**
     * 根据id修改数据
     * @param subject
     * @return
     */
    @Override
    public Result updateById(Subject subject) {
        subjectMapper.updateById(subject);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id) {
        int i = subjectMapper.deleteById(id);
        if (i==1){
            return Result.success();
        }
        return Result.error("删除失败，请稍后再试！");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(List<Integer> ids) {
        subjectMapper.deleteByIds(ids);
        return Result.success();
    }
}
